
# 首先安装pygame模块
# -*- coding:utf-8 -*-

import pygame
import time
from pygame.locals import *

class HeroPlane(object):
    def __init__(self,screen_temp):
        # 3. 创建一个飞机图片
        self.image = pygame.image.load("./feiji/hero1.png")
        self.screen = screen_temp
        self.x = 160
        self.y = 500

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move_left(self):
        self.x -=5
    def move_right(self):
        self.x +=5

def key_control(hero_temp):
    # 获取事件，比如按键等  #因为使用vi打开粘贴代码可能顺序乱了，我们安装sunblime，使用subl file.python打开粘贴就不乱了
    for event in pygame.event.get():

        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                hero_temp.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                hero_temp.move_right()
            # 检测按键是否是空格键
            elif event.key == K_SPACE:
                print('space')

def main():
    #1.创建窗口
    screen = pygame.display.set_mode((480,652),0,32)

    #2.创建一个背景图片
    background = pygame.image.load("./feiji/background.png")

    # 创建一个飞机对象
    hero = HeroPlane(screen)

    while True:
        screen.blit(background,(0,0))
        hero.display()
        pygame.display.update()
        time.sleep(0.01)
        key_control(hero)

if __name__ == "__main__":
    main()